//
//  SNHStaticTableViewCellInfo.m
//  Pods
//
//  Created by 陈建蕾 on 2018/6/9.
//

#import "SNHStaticTableViewCellInfo.h"
#import "SNHStaticTableView.h"
#import "SNHStaticTableViewCell.h"

@implementation SNHStaticTableViewCellInfo

- (instancetype)init {
    if (self = [super init]) {
        [self _config];
    }
    return self;
}

- (void)_config {
    _selectionStyle = UITableViewCellSelectionStyleDefault;
    _accessoryType = UITableViewCellAccessoryNone;
    _cellStyle = UITableViewCellStyleValue1;
}

+ (instancetype)defaultCellInfo {
    return [SNHStaticTableViewCellInfo new];
}

+ (instancetype)normalCellForTitle:(NSString *)title rightValue:(NSString *)rightValue {
    SNHStaticTableViewCellInfo *cellInfo = [self defaultCellInfo];
    cellInfo.selectionStyle = UITableViewCellSelectionStyleNone;
    cellInfo.height = 44.f;
    [cellInfo setUserInfoValue:title forKey:@"title"];
    [cellInfo setUserInfoValue:rightValue forKey:@"rightValue"];
    return cellInfo;
}

+ (instancetype)normalCellForTitle:(NSString *)title rightValue:(NSString *)rightValue imageName:(NSString *)imageName {
    SNHStaticTableViewCellInfo *cellInfo = [self normalCellForTitle:title rightValue:rightValue];
    [cellInfo setUserInfoValue:imageName forKey:@"imageName"];
    return cellInfo;
}

+ (instancetype)normalCellForTarget:(id)target action:(SEL)action title:(NSString *)title rightValue:(NSString *)rightValue accessoryType:(UITableViewCellAccessoryType)accessoryType {
    SNHStaticTableViewCellInfo *cellInfo = [self normalCellForTitle:title rightValue:rightValue];
    cellInfo.target = target;
    cellInfo.action = action;
    cellInfo.accessoryType = accessoryType;
    cellInfo.selectionStyle = UITableViewCellSelectionStyleDefault;
    return cellInfo;
}

+ (instancetype)normalCellForTarget:(id)target action:(SEL)action title:(NSString *)title rightValue:(NSString *)rightValue imageName:(NSString *)imageName accessoryType:(UITableViewCellAccessoryType)accessoryType {
    SNHStaticTableViewCellInfo *cellInfo = [self normalCellForTarget:target action:action title:title rightValue:rightValue accessoryType:accessoryType];
    [cellInfo setUserInfoValue:imageName forKey:@"imageName"];
    return cellInfo;
}

+ (instancetype)centerCellForTarget:(id)target action:(SEL)action title:(NSString *)title {
    SNHStaticTableViewCellInfo *cellInfo = [self defaultCellInfo];
    cellInfo.makeTarget = cellInfo;
    cellInfo.makeAction = @selector(makeCenterCell:);
    cellInfo.target = target;
    cellInfo.action = action;
    cellInfo.height = 44.f;
    cellInfo.accessoryType = UITableViewCellAccessoryNone;
    cellInfo.cellStyle = UITableViewCellStyleDefault;
    [cellInfo setUserInfoValue:title forKey:@"title"];
    return cellInfo;
}

+ (instancetype)switchCellForTarget:(id)target action:(SEL)action title:(NSString *)title isOn:(BOOL)isOn {
    SNHStaticTableViewCellInfo *cellInfo = [self defaultCellInfo];
    cellInfo.makeTarget = cellInfo;
    cellInfo.makeAction = @selector(makeSwitchCell:);
    cellInfo.height = 44.f;
    cellInfo.selectionStyle = UITableViewCellSelectionStyleNone;
    cellInfo.accessoryType = UITableViewCellAccessoryNone;
    [cellInfo setUserInfoValue:title forKey:@"title"];
    [cellInfo setUserInfoValue:@(isOn) forKey:@"on"];
    cellInfo.targetForSwitch = target;
    cellInfo.action = action;
    return cellInfo;
}

+ (instancetype)makeCellForTarget:(id)targer action:(SEL)action makeTarget:(id)makeTarget makeAction:(SEL)makeAction height:(CGFloat)height accessoryType:(UITableViewCellAccessoryType)accessoryType userInfo:(NSDictionary *)userInfo {
    SNHStaticTableViewCellInfo *cellInfo = [self defaultCellInfo];
    cellInfo.makeTarget = makeTarget;
    cellInfo.makeAction = makeAction;
    cellInfo.target = targer;
    cellInfo.action = action;
    cellInfo.height = height;
    cellInfo.accessoryType = accessoryType;
    cellInfo.userInfo = userInfo;
    return cellInfo;
}

#pragma mark - Action
- (void)makeCenterCell:(SNHStaticTableViewCell *)cell {
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
}

- (void)makeSwitchCell:(SNHStaticTableViewCell *)cell {
    NSNumber *isOn = [self getUserInfoForKey:@"on"];
    NSNumber *switchTag = [self getUserInfoForKey:@"switchTag"];
    
    UISwitch *aSwitch = [[UISwitch alloc] init];
    cell.accessoryView = aSwitch;
    [aSwitch addTarget:self action:@selector(onActionSwitchCell:) forControlEvents:UIControlEventValueChanged];
    aSwitch.on = isOn.boolValue;
    aSwitch.tag = switchTag.integerValue;
    cell.selectionStyle = self.selectionStyle;
}

- (void)onActionSwitchCell:(UISwitch *)sender {
    [self setUserInfoValue:@(sender.isOn) forKey:@"on"];
    if ([self.targetForSwitch respondsToSelector:self.action]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self.targetForSwitch performSelector:self.action withObject:self withObject:self.indexPath];
#pragma clang diagnostic pop
    }
}
@end
